//
//  LDYComposePickerView.swift
//  李东阳微博
//
//  Created by 李东阳 on 16/4/24.
//  Copyright © 2016年 李东阳. All rights reserved.
//

import UIKit
let LDYComposePickerViewCellId = "LDYComposePickerViewCellId"
class LDYComposePickerView: UICollectionView {
  //告知外界弹出图片控制器
    var  AddButtonClosure:(() -> ())?
    
    var images: [UIImage] = [UIImage]()
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
    
    //MARK: - 供外界调用的方法
    func addImage(image: UIImage){
  
      images.insert(image, atIndex: 0)
        
        if images.count > 9 {
            
        images.removeLast()
        }
        if images.count > 0 {
    
        self.hidden = false
        }
        reloadData()
    }

    
    //MARK: -  设置视图
    private func setupUI(){
    
     self.backgroundColor = UIColor.whiteColor()
        //刚进来 隐藏配图
    self.hidden = true

    dataSource = self
    delegate = self
        
    //注册cell
        registerClass(LDYComposePickerViewCell.self, forCellWithReuseIdentifier: LDYComposePickerViewCellId)
        
        let margin: CGFloat =  5
        let cellWh: CGFloat = (UIScreeonwidth - 20 - 2 * margin)/3
   //获取layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
      
        //设置itemsize
        layout.itemSize = CGSize(width: cellWh, height: cellWh)
    
        //设置间距
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
    }

}

extension LDYComposePickerView: UICollectionViewDataSource, UICollectionViewDelegate{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = images.count
        if count == 0 || count == 9 {
        
        return count
        }else{
        
        return count + 1
        }

}
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LDYComposePickerViewCellId, forIndexPath: indexPath) as!LDYComposePickerViewCell
        
    
        
        if indexPath.item == images.count {
        
        cell.cellImage = nil
        }else{
        
    cell.cellImage = images[indexPath.item]
        }
        
        cell.composePickerViewClosure = { [weak self] in
            
       self?.images.removeAtIndex(indexPath.item)
          
            if self?.images.count == 0 {
            
            self?.hidden = true
            }
        self!.reloadData()
        }
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
     collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if indexPath.item == images.count{
        
           AddButtonClosure?()
        }
    }

}
//MARK: - 自定义cell

class LDYComposePickerViewCell: UICollectionViewCell {
    
    var composePickerViewClosure: (() -> ())?
    
    var cellImage: UIImage? {
    
    didSet{

        if cellImage != nil {
            
           cellImageView.image = cellImage
            
           cellImageView.highlightedImage = nil
            
                 deleteButton.hidden = false
        }else{
        
            cellImageView.image = UIImage(named: "compose_pic_add")
            // 解决cell复用问题
            cellImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
            
            deleteButton.hidden = true
        }
        
    }
    
}
    
   override init(frame: CGRect) {
        super.init(frame: frame)
    
      setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
    
        //添加控件
    contentView.addSubview(cellImageView)
    contentView.addSubview(deleteButton)
    //添加约束
      cellImageView.snp_makeConstraints { (make) -> Void in
        
        make.top.bottom.leading.trailing.equalTo(self)
        }
     
      deleteButton.snp_makeConstraints { (make) -> Void in
        
       make.top.equalTo(contentView)
       make.trailing.equalTo(contentView)
        }
    }
    
    //MARK: - 按钮的点击事件
    @objc func deleteButtonClick(){
    
    composePickerViewClosure!()
    
    }
    
    //MARK: - 懒加载控件
    private lazy var cellImageView: UIImageView = UIImageView()
    
    //MARK: - 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return button
    }()

    
}



